# !/usr/bin/env python
# -*- coding:utf-8 -*-
# project name: agent_interface_automation
# author: "Lei Yong"
# creation time: 2020-11-26 14:34
# Email: leiyong711@163.com


def to_jsonschema(json_data, result):
    """
    递归生成jsonschema
    :param json_data: json响应数据
    :param result: []空数组用来存储生成的验证模型
    :return:
    """

    if isinstance(json_data, dict):
        is_null = True
        result.append('{')
        result.append("'type': 'object',")
        result.append("'properties': {")
        for k, v in json_data.items():
            is_null = False
            result.append("'%s':" % k)
            to_jsonschema(v, result)
            result.append(',')
        if not is_null:
            result.pop()
        result.append('}')
        result.append('}')
    elif isinstance(json_data, list):
        result.append('{')
        result.append("'type': 'array',")
        result.append("'items': ")
        to_jsonschema(json_data[0], result)
        result.append('}')
    elif isinstance(json_data, bool):
        result.append("{")
        result.append("'type': 'boolean'")
        result.append('}')
    elif isinstance(json_data, int):
        result.append("{")
        result.append("'type': 'number'")
        result.append('}')
    elif isinstance(json_data, float):
        result.append("{")
        result.append("'type': 'number'")
        result.append('}')
    elif isinstance(json_data, str):
        result.append("{")
        result.append("'type': 'string'")
        result.append('}')
    elif not json_data:
        result.append("{")
        result.append("'type': 'null'")
        result.append("}")

    return "".join(result)
